package com.pshcp.patient.tool;

import cn.hutool.core.date.DateUtil;
import com.external.ws.soap.entity.dto.GetSchedulingDTO;
import com.external.ws.soap.entity.dto.RemainQueryDTO;
import com.external.ws.soap.entity.dto.RemainSchedulingId;
import com.external.ws.soap.entity.res.Remain;
import com.external.ws.soap.entity.res.Scheduling;
import com.framework.base.PageList;
import com.framework.thread.ReqComHold;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.DayTypeEnum;
import com.pshcp.common.enums.DoctorEvaTypeEnum;
import com.pshcp.common.enums.DoctorLevelEnum;
import com.pshcp.common.tool.PhotoTools;
import com.pshcp.domain.dto.DoctorDTO;
import com.pshcp.domain.dto.DoctorDiagnosisEvaluationDTO;
import com.pshcp.domain.dto.DoctorScheduleDTO;
import com.pshcp.domain.model.Dept;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.DoctorDiagnosisEvaluation;
import com.pshcp.domain.model.DoctorSchedule;
import com.pshcp.domain.model.Patient;
import com.pshcp.domain.vo.DeptVO;
import com.pshcp.patient.domain.doctor.ao.DoctorDescAO;
import com.pshcp.patient.domain.doctor.ao.DoctorEvaAddAO;
import com.pshcp.patient.domain.doctor.ao.DoctorEvaListAO;
import com.pshcp.patient.domain.doctor.ao.DoctorIntroAO;
import com.pshcp.patient.domain.doctor.ao.DoctorScheduleAO;
import com.pshcp.patient.domain.doctor.vo.DoctorEvaListVO;
import com.pshcp.patient.domain.doctor.vo.DoctorIntroVO;
import com.pshcp.patient.domain.doctor.vo.DoctorScheduleVO;
import com.pshcp.patient.domain.doctor.vo.RegHomeVO;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 患者端-医生类接口属性
 */
public class DoctorTools {

    /**
     * 构建排班访问入参
     * @param scheduleAO
     * @return
     */
    public static DoctorScheduleDTO buildScheduleAO2DTO(DoctorScheduleAO scheduleAO){
        DoctorScheduleDTO scheduleDTO = new DoctorScheduleDTO();
        if(!ObjectUtils.isEmpty(scheduleAO.getDeptId())){
            scheduleDTO.setAccessDeptId(scheduleAO.getDeptId());
        }
        if(!ObjectUtils.isEmpty(scheduleAO.getHisDoctorId())){
            scheduleDTO.setAccessDoctorId(scheduleAO.getHisDoctorId());
        }
        if(!ObjectUtils.isEmpty(scheduleAO.getSchDate())){
            scheduleDTO.setSchDate(scheduleAO.getSchDate());
        }
        if(!ObjectUtils.isEmpty(scheduleAO.getExpert())){
            scheduleDTO.setIsExpect(scheduleAO.getExpert()+"");
        }
        if(!ObjectUtils.isEmpty(scheduleAO.getSchDay())){
            scheduleDTO.setStartTime(scheduleAO.getSchDate());
            Date schD = DateUtil.parseDate(scheduleAO.getSchDate());
            scheduleDTO.setEndTime(DateUtil.formatDate(DateUtil.offsetDay(schD, scheduleAO.getSchDay())));
            scheduleDTO.setSchDate(null);
        }
        return scheduleDTO;
    }

    public static RemainQueryDTO buildWsRemainQueryDTO(List<DoctorSchedule> schedulings, DoctorScheduleAO scheduleAO){
        RemainQueryDTO dto = new RemainQueryDTO();
        dto.setAccessDeptId("");
        if(!ObjectUtils.isEmpty(scheduleAO.getDeptId())){
            dto.setAccessDeptId(scheduleAO.getDeptId());
        }
        dto.setState("");
        dto.setSchId("");
        dto.setStartTime("");
        dto.setEndTime("");
        dto.setHosDistId("");
        List<RemainSchedulingId> rs = new ArrayList<>();
        schedulings.forEach(sd -> {
            RemainSchedulingId scheduling = new RemainSchedulingId();
            scheduling.setAccessSchId(sd.getAccessSchId());
            rs.add(scheduling);
        });
        dto.setSchedulings(rs);
        return dto;
    }

    public static void buildScheduleRemainInfo(List<DoctorScheduleVO> voList, List<Remain> remains){
        Map<String, Integer> schIdAndStateMap = remains.stream().collect(Collectors.toMap(Remain::getAccessSchId, Remain::getState));
        voList.forEach(vo -> {
            Integer state = schIdAndStateMap.get(vo.getAccessSchId());
            vo.setState(state == null ? 0 : state);
        });
    }

    /**
     * 构建ws排班对象到vo
     * @param schedulings
     * @return
     */
    public static List<DoctorScheduleVO> buildScheduleVOS(List<DoctorSchedule> schedulings){
        if(CollectionUtils.isEmpty(schedulings)){
            return Collections.emptyList();
        }
        List<DoctorScheduleVO> scheduleVOS = new ArrayList<>();
        schedulings.forEach(s -> {
            DoctorScheduleVO scheduleVO = new DoctorScheduleVO();
            scheduleVO.setId(s.getId());
            scheduleVO.setAccessSchId(s.getAccessSchId());
            scheduleVO.setDeptId(s.getAccessDeptId());
            scheduleVO.setDeptName(s.getDeptName());
            scheduleVO.setHisDoctorId(s.getAccessDoctorId());
            scheduleVO.setDoctorName(s.getDoctorName());
            scheduleVO.setSchDate(s.getSchDate());
            scheduleVO.setStartTime(s.getStartTime());
            scheduleVO.setEndTime(s.getEndTime());
            scheduleVO.setDayType(s.getDayType());
            scheduleVO.setWeekType(s.getWeekType());
            scheduleVO.setPreTime(s.getPreTime());
            scheduleVO.setClinicAddr(s.getClinicAddr());
            scheduleVO.setCost(s.getCost());
            scheduleVO.setRegFee(s.getRegFee());
            scheduleVO.setServiceFee(s.getServiceFee());
            scheduleVO.setIsExpect(s.getIsExpect());
            scheduleVO.setNumSrcType(s.getNumSrcType());
            scheduleVO.setRemain(s.getRemain());
            scheduleVO.setResNo(s.getResNo());
            scheduleVO.setSchLevel(s.getSchLevel());
            scheduleVO.setState(s.getState());
            scheduleVOS.add(scheduleVO);
        });
        return scheduleVOS;
    }

    public static RegHomeVO buildRegHomeVO(Doctor doctor, String prefixPath){
        RegHomeVO regHomeVO = CommonTool.copyProperties(doctor, RegHomeVO.class);
        regHomeVO.setDeptId(doctor.getMainDeptId());
        regHomeVO.setDeptName(doctor.getMainDeptName());
        regHomeVO.setFullHeadPhotoPath(PhotoTools.getFullPath(prefixPath, doctor.getHeadPhoto()));
        regHomeVO.setOnlineNum(doctor.getOnlineNum());
        return regHomeVO;
    }

    public static void buildRegHomeVODept(RegHomeVO regHomeVO, List<Dept> depts){
        if(CollectionUtils.isEmpty(depts)){
            return;
        }
        regHomeVO.setDepts(CommonTool.copyPropertiesList(depts, DeptVO.class));
    }

    public static void buildIntroVODepts(PageList<DoctorIntroVO> pageList, Map<Long, List<Dept>> doctorIdAndDeptsMap){
        pageList.getItems().forEach(vo -> {
            List<Dept> depts = doctorIdAndDeptsMap.get(vo.getId());
            vo.setDepts(CommonTool.copyPropertiesList(depts, DeptVO.class));
        });
    }

    public static DoctorDTO buildDoctorDTO(DoctorIntroAO introAO){
        DoctorDTO doctorDTO = new DoctorDTO();
        doctorDTO.setPageNum(introAO.getPageNum());
        doctorDTO.setPageSize(introAO.getPageSize());
        doctorDTO.setSearch(introAO.getSearch());
        doctorDTO.setDeptId(introAO.getDeptId());
        if(introAO.getMediLevel() != null){
            doctorDTO.setTitleShown(DoctorLevelEnum.getDescByCode(introAO.getMediLevel()));
        }
        doctorDTO.setSortType(introAO.getSortType());
        return doctorDTO;
    }

    public static DoctorDTO buildDoctorDTO(DoctorDescAO doctorDescAO){
        DoctorDTO doctorDTO = new DoctorDTO();
        doctorDTO.setPageNum(doctorDescAO.getPageNum());
        doctorDTO.setPageSize(doctorDescAO.getPageSize());
        doctorDTO.setSearch(doctorDescAO.getSearch());
        if(doctorDescAO.getMediLevel() != null){
            doctorDTO.setTitleShown(DoctorLevelEnum.getDescByCode(doctorDescAO.getMediLevel()));
        }
        return doctorDTO;
    }

    public static DoctorDiagnosisEvaluation buildEvaAddAO2Model(DoctorEvaAddAO evaAddAO, Patient patient){
        DoctorDiagnosisEvaluation evaluation = CommonTool.copyProperties(evaAddAO, DoctorDiagnosisEvaluation.class);
        evaluation.setPatientUserId(ReqComHold.getReqCom().getUserId());
        evaluation.setPatientName(patient.getName());
        evaluation.setType(DoctorEvaTypeEnum.ONLINE.getCode());
        evaluation.setDataId(evaAddAO.getOnlineOrderId());
        if(!CollectionUtils.isEmpty(evaAddAO.getLabels())){
            String labels = evaAddAO.getLabels().stream().collect(Collectors.joining(","));
            evaluation.setLabels(labels);
        }
        return evaluation;
    }

    public static PageList<DoctorEvaListVO> buildEvaListModel2VO(PageList<DoctorDiagnosisEvaluation> evaluationPageList){
        PageList<DoctorEvaListVO> voPageList = CommonTool.buildPageList(evaluationPageList, DoctorEvaListVO.class);
        if(CollectionUtils.isEmpty(evaluationPageList.getItems())){
            return voPageList;
        }
        List<DoctorEvaListVO> voList = new ArrayList<>();
        evaluationPageList.getItems().forEach(eva -> {
            DoctorEvaListVO vo = CommonTool.copyProperties(eva, DoctorEvaListVO.class);
            vo.setLabels(Arrays.asList(eva.getLabels().split(",")));
            voList.add(vo);
        });
        voPageList.setItems(voList);
        return voPageList;
    }

    public static DoctorDiagnosisEvaluationDTO buildEvaluation(DoctorEvaListAO listAO){
        DoctorDiagnosisEvaluationDTO dto = CommonTool.copyProperties(listAO, DoctorDiagnosisEvaluationDTO.class);
        if(listAO.getType() == null || listAO.getType().intValue() == 0){
            dto.setAfterTime(DateUtil.offsetMonth(new Date(), -6));
            dto.setPageNum(1);
            dto.setPageSize(50);
        }
        return dto;
    }

}
